package com.cqz.action;

import com.cqz.dao.UserInfo;
import com.cqz.services.UserService;
import com.cqz.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class RegisterAction {

    @Autowired
    private UserService userService;

    /**
     * 注册
     * @param httpSession
     * @param user_name
     * @param user_password
     * @return
     */
    @RequestMapping("registerAction")
    public String register(HttpSession httpSession,String user_name,String user_password){
        httpSession.removeAttribute("userNameOrPassword");
        String flag="0";
        if(Tools.isNull(user_name)){
            return "login";
        }else if(Tools.isNull(user_password)){
            return "login";
        }else {
            flag=userService.findUser(user_name);
            if(!flag.equals("0")){
                httpSession.setAttribute("userNameOrPassword","该用户名已被注册");
                return "login";
            }else {
                userService.addUser(user_name,user_password);
                UserInfo userInfo=userService.checkUser(user_name,user_password);
                httpSession.setAttribute("user_name", user_name);
                httpSession.setAttribute("isLogin",true);
                httpSession.setAttribute("sessionid",httpSession.getId());
                httpSession.setAttribute("state",flag);
                httpSession.setAttribute("uid",userInfo.getUid());
                httpSession.setAttribute("money",0);
                httpSession.setAttribute("tag_register","1");
                httpSession.setAttribute("address",userInfo.getUaddress());
                return "redirect:tagAction";
            }
        }
    }

}
